require 'watir'
include Watir
require 'test/unit'
require '../watir.config.rb'
require '../resources/functions.rb'
require 'watir/testcase'

class TC_EditProfile_test < Test::Unit::TestCase
  
    def startup
        $UserName ='User123'
        $UserPassword ='pass123'
        $UserEmail = 'user123@user.com'
        $FilePath =  '../resources/Images/gallery_slideshow.gif'
        $Bio='A brief Bio of the user'
        $Name = 'Sajid Mohammed'
        $Location = 'India'
        $BirthDay = 'Jan 21, 1978'
        $WebAddress ='http://www.greatdeccan.com'
        $WebLog ='http://www.greatdeccan.blogspot.com'
        $WebGallery = 'http://www.greatdeccan.yahoophotos.com'
        $PublicEmail = 'sajid.greatdeccan@mailingservice.com'
        $RSS= 'Testing RSS Feeds /n'
        $PrivateEmail   =    $UserEmail
        $Signature      =    'Regards \nSajid'
        $ApiKeyName1 =    'Key1'
        $ApiKeyName1Edit = 'ApiKeyEdit'
        $Password1     =    'pass12345'
        @@ie = Watir::IE.start($SITE_URL)
      end
    
    def ie
      if defined? @@ie
        @@ie
      else
        startup
      end
    end
    
    
    def test_01_verifyHomeText
        assert(ie.contains_text(/Community Server/))		
    end
      
    def test_02_verifyLoginLinksExist
      if !(ie.link(:text, 'Sign in').exists?)
        ie.goto($SITE_URL + 'logout.aspx')
        ie.refresh
        ie.goto($SITE_URL)
        ie.refresh
      end
	  end
    
    def test_03_navigateToLogin
        ie.link(:text, 'Sign in').click
        assert(ie.contains_text("Sign in name"))
    end
	  
      def test_04_populateLoginForm
        ie.text_field(:id, /username$/).value = $ADMIN_USER
        assert(ie.text_field(:id, /username$/).verify_contains($ADMIN_USER))
      
        ie.text_field(:id, /password$/).value = $ADMIN_PASSWORD
        assert(ie.text_field(:id, /password$/).verify_contains($ADMIN_PASSWORD))
	  end
	  
   def test_05_submitLoginForm
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        test_01_verifyHomeText
      end
    
    
    def test_06_verifyLinks
        assert_equal $SITE_URL , ie.link(:text, 'Home').href
        assert_equal $SITE_URL+'blogs/', ie.link(:text, 'Blogs').href
        assert_equal $SITE_URL+'media/', ie.link(:text, 'Media').href
        assert_equal $SITE_URL+'forums/', ie.link(:text, 'Forums').href
        assert_equal $SITE_URL+'groups/', ie.link(:text, 'Groups').href
        assert_equal $SITE_URL+'controlpanel/default.aspx', ie.link(:text, 'Control Panel').href
      end
      
     def test_07_navigateToCreateUserAccount
        ie.goto($SITE_URL +'controlpanel/membership/UserAdd.aspx')
        assert(ie.text_field(:id,/username$/).exists?)
        ie.text_field(:id,/username$/).value = $UserName
        assert(ie.text_field(:id,/password$/).exists?)
        ie.text_field(:id,/password$/).value = $UserPassword
        password=ie.text_field(:id,/password$/).value
        assert(ie.text_field(:id,/password2$/).exists?)
        ie.text_field(:id,/password2$/).value =password
        assert(ie.text_field(:id,/email$/).exists?)
        ie.text_field(:id,/email$/).value = $UserEmail
        assert(ie.select_list(:id,/timezone$/).exists?)
        ie.select_list(:id,/timezone$/).select('(GMT +05:00) Islamabad, Karachi, Tashkent')
        assert(ie.button(:name,/createButton$/).exists?)
        ie.button(:name,/createButton$/).click
      end
      
    def test_08_navigateToProfileOptions
        test_02_verifyLoginLinksExist
        test_03_navigateToLogin
        ie.text_field(:id, /username$/).value = $UserName
        ie.text_field(:id, /password$/).value = $UserPassword
        if(ie.button(:id, /loginButton$/).exists?)
          ie.button(:id, /loginButton$/).click
        else
        ie.link(:id, /loginButton$/).click
        end
        sleep 5
        assert(ie.link(:text,'Home').exists?)
        ie.link(:text,'Home').click
        assert(ie.link(:text,'Edit profile').exists?)
        ie.link(:text,'Edit profile').click
        # Avatar
        assert(ie.radio(:id,/EnableAvatar_Yes$/).exists?)
        assert(ie.radio(:id,/EnableAvatar_No$/).exists?)
        ie.radio(:id,/EnableAvatar_Yes$/).set
        assert(ie.fileField(:id,/AvatarUpload$/).exists?)
        ie.fileField(:id,/AvatarUpload$/).set($FilePath)
        assert(ie.button(:id,/SubmitAvatar$/).exists?)
        ie.button(:id,/SubmitAvatar$/).click
        # Bio
        sleep 2
        ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_bcr_EditUserForm1_ctl02_bio_ctl00_Editor').execCommand('mceSetContent', false,'"+$Bio+"')")
        # Common Profile Options
        assert(ie.text_field(:id ,/CommonName$/).exists?)
        ie.text_field(:id ,/CommonName$/).value = $Name
        assert(ie.text_field(:id ,/Location$/).exists?)
        ie.text_field(:name ,/Location$/).value = $Location
        assert(ie.radio(:id,/EnableBirthday$/).exists?)
        ie.radio(:id,/EnableBirthday$/).set
        assert(ie.text_field(:id ,/Birthday_text$/).exists?)
        ie.text_field(:id ,/Birthday_text$/).value = $Birthday
        assert(ie.radio(:id,/Gender_1$/).exists?)
        ie.radio(:id,/Gender_1$/).set
        assert(ie.text_field(:id ,/WebAddress$/).exists?)
        ie.text_field(:id ,/WebAddress$/).value = $WebAddress
        assert(ie.text_field(:id ,/WebLog$/).exists?)
        ie.text_field(:id ,/WebLog$/).set($WebLog)
        assert(ie.text_field(:id ,/WebGallery$/).exists?)
        ie.text_field(:id ,/WebGallery$/).set($WebGallery)
        assert(ie.select_list(:id,/Language$/).exists?)
        ie.select_list(:id,/Language$/).select('U.S. English')
        assert(ie.text_field(:id ,/PublicEmail$/).exists?)
        ie.text_field(:id ,/PublicEmail$/).value = $PublicEmail
        assert(ie.radio(:id,/ShareFavoritePosts_Yes$/).exists?)
        ie.radio(:id,/ShareFavoritePosts_Yes$/).set
        assert(ie.radio(:id,/ShareFavoriteSections_Yes$/).exists?)
        ie.radio(:id,/ShareFavoriteSections_Yes$/).set
        # Recommended Reading
        sleep 4
        assert(ie.text_field(:name ,'ctl00$bcr$EditUserForm1$ctl02$rssFeeds').exists?)
        sleep 4
        ie.text_field(:name ,'ctl00$bcr$EditUserForm1$ctl02$rssFeeds').value = $RSS
      end  
    def test_09_navigateToSiteOptions
        # Site Options
	      ie.ie.Document.parentWindow.execScript("ctl00_bcr_EditUserForm1_ctl02_ProfileTabs.SelectTab(ctl00_bcr_EditUserForm1_ctl02_ProfileTabs.GetTabAtIndex(1))")
        assert(ie.select_list(:id,/SortOrder$/).exists?)
        ie.select_list(:id,/SortOrder$/).select('Descending')
        assert(ie.select_list(:id,/FontSize$/).exists?)
        ie.select_list(:id,/FontSize$/).select('Large')
        assert(ie.select_list(:id,/EditorList$/).exists?)
        ie.select_list(:id,/EditorList$/).select('Standard')
        assert(ie.select_list(:id,/PostViewType$/).exists?)
        ie.select_list(:id,/PostViewType$/).select('Threaded View')
        assert(ie.radio(:id,/enableDisplayNames_Yes$/).exists?)
        ie.radio(:id,/enableDisplayNames_Yes$/).set
        assert(ie.radio(:id,/DisplayInMemberList_Yes$/).exists?)
        ie.radio(:id,/DisplayInMemberList_Yes$/).set
        assert(ie.radio(:id,/EnablePostPreviewPopup_Yes$/).exists?)
        ie.radio(:id,/EnablePostPreviewPopup_Yes$/).set
        assert(ie.radio(:id,/EnableCollapsingPanels_Yes$/).exists?)
        ie.radio(:id,/EnableCollapsingPanels_Yes$/).set
        assert(ie.radio(:id,/EnableConversationNotifications_Yes$/).exists?)
        ie.radio(:id,/EnableConversationNotifications_Yes$/).set
        assert(ie.radio(:id,/EnableCommentNotifications_Yes$/).exists?)
        ie.radio(:id,/EnableCommentNotifications_Yes$/).set
        assert(ie.radio(:id,/EnableEmoticons_Yes$/).exists?)
        ie.radio(:id,/EnableEmoticons_Yes$/).set
        assert(ie.radio(:id,/enableUserAvtars_Yes$/).exists?)
        ie.radio(:id,/enableUserAvtars_Yes$/).set
        assert(ie.radio(:id,/enableUserSignatures_Yes$/).exists?)
        ie.radio(:id,/enableUserSignatures_Yes$/).set
        assert(ie.select_list(:id,/PageSize$/).exists?)
        ie.select_list(:id,/PageSize$/).select('20')
        # Timezone
        assert(ie.select_list(:id,/Timezone$/).exists?)
        ie.select_list(:id,/Timezone$/).select('(GMT +07:00) Bangkok, Hanoi, Jakarta')
        # Date Format
        assert(ie.select_list(:id,/DateFormat$/).exists?)
        ie.select_list(:id,/DateFormat$/).select('Day, Month Days Year (Thursday, June 1 2003)')
        # Email Configuration
        assert(ie.text_field(:id ,/PrivateEmail$/).exists?)
        ie.text_field(:id ,/PrivateEmail$/).value = $PrivateEmail
        assert(ie.radio(:id,/ReceiveEmails_Yes$/).exists?)
        ie.radio(:id,/ReceiveEmails_Yes$/).set
        assert(ie.radio(:id,/EnableHtmlEmail_Yes$/).exists?)
        ie.radio(:id,/EnableHtmlEmail_Yes$/).set
        assert(ie.radio(:id,/EnableEmailTracking_Yes$/).exists?)
        ie.radio(:id,/EnableEmailTracking_Yes$/).set
        # Signature 
        sleep 2
	      ie.ie.Document.ParentWindow.execscript("tinyMCE.get('ctl00_bcr_EditUserForm1_ctl02_Signature_ctl00_Editor').execCommand('mceSetContent', false,'"+$Signature+"')")
        assert(ie.button(:id,/UpdateButtonBottom$/).exists?)
        ie.button(:id,/UpdateButtonBottom$/).click
        sleep 5
        # My API Keys
	      ie.ie.Document.parentWindow.execScript("ctl00_bcr_EditUserForm1_ctl02_ProfileTabs.SelectTab(ctl00_bcr_EditUserForm1_ctl02_ProfileTabs.GetTabAtIndex(1))")
        sleep 2
	      assert(ie.link(:text,'Create and Edit Application Keys').exists?)
        ie.link(:text,'Create and Edit Application Keys').click
        assert(ie.text_field(:id,/ApiKeyName$/).exists?)
        ie.text_field(:id,/ApiKeyName$/).value = $ApiKeyName1
        assert(ie.button(:id,/GenerateApiKey$/).exists?)
        ie.button(:id,/GenerateApiKey$/).click
	      # Edit AppKey
        assert(ie.table(:index,3).exists?)
        table1 = ie.table(:index,3)
        rowcount = table1.row_count
        rowcount1 =1
        while rowcount1<=rowcount
            if (table1[rowcount1][1].text == $ApiKeyName1 )
               sleep 5
               table1[rowcount1][5].link(:text, 'Edit').click
               break
             end   
         rowcount1=rowcount1+1
       end
       assert(ie.frame(:index,1).text_field(:id,/ApiKeyName$/).exists?)
       ie.frame(:index,1).text_field(:id,/ApiKeyName$/).set($ApiKeyName1Edit)
       assert(ie.frame(:index,1).radio(:id,/IsEnabled_Yes$/).exists?)
       ie.frame(:index,1).radio(:id,/IsEnabled_Yes$/).set
       assert(ie.frame(:index,1).button(:id,/SaveApiKey$/).exists?)
       ie.frame(:index,1).button(:id,/SaveApiKey$/).click
       #Delete Key
       table1 = ie.table(:index,3)
        rowcount = table1.row_count
        rowcount1 =1
        while rowcount1<=rowcount
            if (table1[rowcount1][1].text == $ApiKeyName1Edit )
               sleep 5
               table1[rowcount1][5].link(:text, 'Delete').click
               break
             end   
         rowcount1=rowcount1+1
       end
      end
    def test_10_navigateTOSigninInformation
        assert(ie.link(:text,'Home').exists?)
        ie.link(:text,'Home').click
        assert(ie.link(:text,'Edit profile').exists?)
        ie.link(:text,'Edit profile').click
        ie.ie.Document.parentWindow.execScript("ctl00_bcr_EditUserForm1_ctl02_ProfileTabs.SelectTab(ctl00_bcr_EditUserForm1_ctl02_ProfileTabs.GetTabAtIndex(2))")
        assert(ie.link(:text,'Change Password').exists?)
        ie.link(:text,'Change Password').click
        assert(ie.text_field(:id,/Password$/).exists?)
        ie.text_field(:id,/Password$/).value = $UserPassword
        assert(ie.text_field(:id,/Password1$/).exists?)
        ie.text_field(:id,/Password1$/).value = $Password1
        assert(ie.text_field(:id,/Password2$/).exists?)
        ie.text_field(:id,/Password2$/).value = $Password1
        assert(ie.button(:id,/ChangePasswordButton$/).exists?)
        ie.button(:id,/ChangePasswordButton$/).click
        sleep 3
        ie.ie.Document.parentWindow.execScript("ctl00_bcr_EditUserForm1_ctl02_ProfileTabs.SelectTab(ctl00_bcr_EditUserForm1_ctl02_ProfileTabs.GetTabAtIndex(2))")
        sleep 2
        assert(ie.button(:id,/UpdateButtonBottom$/).exists?)
        ie.button(:id,/UpdateButtonBottom$/).click
      end
      
      def  test_11_searchUser
        test_02_verifyLoginLinksExist
        test_03_navigateToLogin
        test_04_populateLoginForm
        test_05_submitLoginForm
        sleep 5
        ie.goto($SITE_URL +'controlpanel/membership/membersearch.aspx?SelectedNavItem=BrowseMembers')
        assert(ie.text_field(:id,/searchText$/).exists?)
        ie.text_field(:id,/searchText$/).value ="*"
        assert(ie.select_list(:name,/PageList$/).exists?)
        ie.select_list(:name,/PageList$/).select('10')
        sleep 3
        assert(ie.button(:id,/searchButton$/).exists?)
        sleep 2
        ie.button(:id,/searchButton$/).click
      end
      
      def test_12_navigateToDeleteUser
        sleep 5
        table1 = ie.table(:index, 6)
        rowcount = table1.row_count
        rowcount1 =1
        ok =0
        while rowcount1<=rowcount
            if (table1[rowcount1][1].link(:text,$UserName).exists?)
              ok =1
               sleep 5
               table1[rowcount1][5].button(:value, 'Delete').click
               sleep 3
               break
             end   
         rowcount1=rowcount1+1
        end
       if(ok ==0)
         sleep 5
         ie.link(:text,'Next >').click
         sleep 5
         test_12_navigateToDeleteUser
       end
       sleep 5
       assert(ie.frame(:index,1).radio(:id,/AssignToAnonymous$/).exists?)
       ie.frame(:index,1).radio(:id,/AssignToAnonymous$/).set
       sleep 3
       assert(ie.frame(:index,1).button(:id,/DeleteUserButton$/).exists?)
       sleep 3
       ie.frame(:index,1).button(:id,/DeleteUserButton$/).click
       sleep 5
       ie.goto($SITE_URL +'controlpanel/membership/membersearch.aspx?SelectedNavItem=BrowseMembers')
       assert(ie.text_field(:id,/searchText$/).exists?)
       ie.text_field(:id,/searchText$/).value ="*"
       sleep 3
       assert(ie.button(:id,/searchButton$/).exists?)
       sleep 2
       ie.button(:id,/searchButton$/).click  
       if(ie.link(:text,$UserName).exists?)
       puts 'User Not Deleted'
       end   
     end
      
      
    def test_99_Exit
        ie.close
    end
end 
    